package cn.topevery.garden.dal.dao.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.topevery.garden.dal.dao.OcaInfoDao;
import cn.topevery.garden.entity.fetch.OcaFetch;
import cn.topevery.garden.entity.po.OcaInfo;
import cn.topevery.garden.entity.query.OcaInfoQuery;
import cn.topevery.garden.entity.vo.OcaInfoVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 绿地占用信息表
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class OcaInfoDaoImpl extends BaseDaoImpl<OcaInfo> implements OcaInfoDao {
    @Override
    public <P extends DbCsp> List<OcaInfo> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<OcaInfoVO> listPage(OcaInfoQuery query) {
        String table = "t_oca_info toi";

        String columns = "toi.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("toi.c_id desc");
        sqlQueryMate
                .where(true, "toi.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, OcaInfoVO.class);
    }

    @Override
    public OcaInfo getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public DataPager<OcaInfo> getList(OcaFetch ocaFetch) {
        String table = "t_oca_info toi";
        String columns = "toi.*";
        int pageIndex = ocaFetch.getPageIndex() - 1;
        int pageSize = ocaFetch.getPageSize();
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotEmpty(ocaFetch.getPermitCode()), "c_permit_code", ScriptConditionEnum.Eq, ocaFetch.getPermitCode().trim())
                .where(StringUtils.isNotEmpty(ocaFetch.getStreetId()), "c_street_id", ScriptConditionEnum.Eq, ocaFetch.getStreetId())
                .where(StringUtils.isNotEmpty(ocaFetch.getDistractId()), "c_distract_id", ScriptConditionEnum.Eq, ocaFetch.getDistractId())
                .where(StringUtils.isNotEmpty(ocaFetch.getDistractId()), "c_oca_address", ScriptConditionEnum.Like, ocaFetch.getDistractId())
                .where(StringUtils.isNotEmpty(ocaFetch.getBeginDate()), "c_permit_date", ScriptConditionEnum.GreaterThan, ocaFetch.getBeginDate())
                .where(StringUtils.isNotEmpty(ocaFetch.getEndDate()), "c_permit_date", ScriptConditionEnum.LessThan, ocaFetch.getEndDate());
        return getDataPager(sqlQueryMate, pageSize, pageIndex, OcaInfo.class);
    }

    @Override
    public OcaInfo getById(Integer id) {
//        String table = "t_oca_info toi";
//        String columns = "toi.*";
//        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
//        sqlQueryMate.where(id != null && id > 0, "c_id", ScriptConditionEnum.Eq, id);
        return getEntity(id);
    }

    @Override
    public void updateByIds(List<Integer> ids) {
        String table = "t_oca_info toi";
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate(table);
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now())
                .setColumnValue(true, "c_db_update_id", UserRuntime.getSysUser().id)
                .where(CollectionUtil.isNotEmpty(ids), "c_id", ScriptConditionEnum.In, ids);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
